package com.wsjc.manager.service.routine.push.impl;

import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wsjc.common.core.constants.CommonConst;
import com.wsjc.common.core.dao.db.TableRoute;
import com.wsjc.common.core.utils.SmsUtil;
import com.wsjc.manager.dao.hospitals.WjHospitalDao;
import com.wsjc.manager.dao.push.WjPushDao;
import com.wsjc.manager.entity.WjHospital;
import com.wsjc.manager.entity.WjPush;
import com.wsjc.manager.service.routine.push.WjPushService;

@Service("wjPushService")
public class WjPushServiceImpl implements WjPushService {

	private Logger logger = LoggerFactory.getLogger(WjPushServiceImpl.class);
	
	@Autowired
	private WjHospitalDao wjHospitalDaoImpl;
	
	@Autowired
	private WjPushDao wjPushDaoImpl;
	
	
	@Override
	public void pushMsg(String detectionResult, String hospitalId, String dataType, String areaId, String smsMsg, String detectionTime) throws Exception{
		
		WjHospital wjHospital = wjHospitalDaoImpl.getById(hospitalId);
		// 验证是否过期 ，是否打开推送 ， 不满足条件不推送
		if (null != wjHospital.getPushSwitch() && wjHospital.getPushSwitch().equals(CommonConst.PUSH_CLOSE)) {
			logger.warn("WSJC-APP-PUSH|WARING|WjPushServiceImpl-pushMsg| the hospital push switch is close..");
			return;
		}
		if (new Date().getTime() > wjHospital.getUseExpiryDate().getTime()) {
			logger.warn("WSJC-APP-PUSH|WARING|WjPushServiceImpl-pushMsg| the hospital Use Expiry Date < today..");
			return;
		}

		/****** 新版短信推送 ********/
		// 不合格 短信推送
	    SmsUtil.sendNoPassData(wjHospital.getTel(), detectionTime, smsMsg); // "13998641323" hospital.getTel() getChinaDate() -- > 2017年06月26日 10时52分53秒
//			SmsUtil.sendNoPassData("13998641323", detectionTime, smsMsg); // "13998641323" hospital.getTel() getChinaDate() -- > 2017年06月26日 10时52分53秒

		WjPush push = new WjPush();
		push.setScope(wjHospital.getAreaId());
		push.setContent(smsMsg);
		push.setIsRead(CommonConst.NO_READ);
		push.setTitle(CommonConst.TITLE_UP_DATA_PUSH);
		push.setDetectionTime(detectionTime);
		push.setHospitalName(wjHospital.getName());
		
		String tableName = TableRoute.getTableName(CommonConst.DATA_TABLE_NAME_PUSH, areaId); 
		push.setTableName(tableName);
		wjPushDaoImpl.insert(push);
		
	}



}
